package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Department;
import com.ruoyi.system.domain.HospitalArea;
import com.ruoyi.system.domain.JobTitle;
import com.ruoyi.system.service.IDepartmentService;
import com.ruoyi.system.service.IHospitalAreaService;
import com.ruoyi.system.service.IJobTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Doctor;
import com.ruoyi.system.service.IDoctorService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * k医生Controller
 * 
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController extends BaseController
{
    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private IHospitalAreaService iHospitalAreaService;
    @Autowired
    private IJobTitleService jobTitleService;

    /**
     * 查询k医生列表
     */
    @RequiresPermissions("system:doctor:list")
    @GetMapping("/list")
    public TableDataInfo list(Doctor doctor)
    {
        startPage();
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        for (Doctor doctor1 : list) {
            Department department = iDepartmentService.selectDepartmentById(doctor1.getDepartmentId());
            doctor1.setDepartmentName(department.getName());

            HospitalArea hospitalArea = iHospitalAreaService.selectHospitalAreaById(doctor1.getHospitalareaId());
            doctor1.setHospitalName(hospitalArea.getName());

            JobTitle jobTitle = jobTitleService.selectJobTitleById(doctor1.getTitleId());
            doctor1.setTitleName(jobTitle.getName());
            doctor1.setPrice(jobTitle.getPrice());

        }
        return getDataTable(list);
    }

    /**
     * 导出k医生列表
     */
    @RequiresPermissions("system:doctor:export")
    @Log(title = "k医生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Doctor doctor)
    {
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        util.exportExcel(response, list, "k医生数据");
    }

    /**
     * 获取k医生详细信息
     */
    @RequiresPermissions("system:doctor:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(doctorService.selectDoctorById(id));
    }

    /**
     * 新增k医生
     */
    @RequiresPermissions("system:doctor:add")
    @Log(title = "k医生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.insertDoctor(doctor));
    }

    /**
     * 修改k医生
     */
    @RequiresPermissions("system:doctor:edit")
    @Log(title = "k医生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.updateDoctor(doctor));
    }

    /**
     * 删除k医生
     */
    @RequiresPermissions("system:doctor:remove")
    @Log(title = "k医生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(doctorService.deleteDoctorByIds(ids));
    }
}
